import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {ElMessage} from "element-plus";
import {loginStatusStore} from "@/stores/counter";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      meta: { index: 0 },
    },
    {
      path: '/note',
      name: 'note',
      component: () => import('../views/NoteView.vue'),
      meta: { index: 1 },
      beforeEnter: (to,from,next) => {
        const store = loginStatusStore()
        let loginStore = JSON.parse(sessionStorage.getItem('LoginStore'))
        if (store.logStatus !== loginStore) {store.logStatus = loginStore}
        if (store.logStatus !== 1) {ElMessage({message: "未登录"}); next('login')}
        else next()
      },
      children: [
        {
          path: '',
          name: 'noteHome',
          component: () => import('../components/Note/NoteMain.vue'),
          meta: { index: 6 }
        },
        {
          path: 'tiptap',
          name: 'tiptap',
          component: () => import('../components/Note/Tiptap/Tiptap.vue'),
          meta: { index: 7 }
        }
      ]
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('../views/PersonView.vue'),
      meta: { index: 2 },
      beforeEnter: (to,from,next) => {
        const store = loginStatusStore()
        let loginStore = JSON.parse(sessionStorage.getItem('LoginStore'))
        if (store.logStatus !== loginStore) {store.logStatus = loginStore}
        if (store.logStatus !== 1) {ElMessage({message: "未登录"}); next('login')}
        else next()
      }
    },
    {
      path: '/learn',
      name: 'learn',
      component: () => import('../views/LearnView.vue'),
      meta: { index: 3 },
      beforeEnter: (to,from,next) => {
        const store = loginStatusStore()
        let loginStore = JSON.parse(sessionStorage.getItem('LoginStore'))
        if (store.logStatus !== loginStore) {store.logStatus = loginStore}
        if (store.logStatus !== 1) {ElMessage({message: "未登录"}); next('login')}
        else next()
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../components/User/Login.vue'),
      meta: { index: 4 },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../components/User/Register.vue'),
      meta: { index: 5 },
    },
    {
      path: '/model',
      name: 'model',
      component: () => import('../components/Learn/model/model.vue'),
      meta: { index: 8}
    },

  ]
})

export default router

